/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import DB_REDIS.Redis_Rd;
import DB_REDIS.Redis_W;
import Model.ModelItem;
import Model.ModelJet;
import Model.Request.ClientRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import libCore.Config;
import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import share.KeysDefinition;
import share.ShareMacros;

/**
 *
 * @author LinhTA
 */
public class JetController extends Controller{
    
     private static Logger logger_ = Logger.getLogger(JetController.class);
    
    @Override  
    public void handle(ClientRequest request, HttpServletResponse resp) 
    {
         try {
            prepareHeader(resp);
            
            if(request._method.equals("buy"))
                buyJet(request, resp);
            else if(request._method.equals("buyLevel"))
                buyLevel(request, resp);
            else if(request._method.equals("get"))
                get(request, resp);
        } catch (Exception ex) {
            logger_.error("Jet.handle:" + ex.getMessage() + ", Username:" + request._uid.toString(), ex);
        }
    }
    
    @Override
    protected JSONObject defaultResponse_False()
    {
        JSONObject data = new JSONObject();
        List<Object> list = new ArrayList<Object>();
        data.put(ShareMacros.SUSSCES,false);
        return  data;
    }
    
    public void buyJet(ClientRequest req, HttpServletResponse resp)
    {
        String uid = req._uid;
        
        String typeJet = "0";        
        if(req._data.containsKey(ShareMacros.TYPE))
            typeJet = req._data.get(ShareMacros.TYPE);
        else
        {
            outFalse(resp);
            return;
        }
        
        Map<String,String> data = new HashMap<String,String>();
        ModelJet modJet = new ModelJet(uid);
        data=  modJet.buyJet(typeJet);
       
        JSONObject json = new JSONObject();
        json.putAll(data);   
        out(json.toJSONString(), resp);
        
    }
    
     public void buyLevel(ClientRequest req, HttpServletResponse resp)
    {
         String uid = req._uid;
         ModelJet modJet = new ModelJet(uid);
         
         String typeJet = "0";
        
        if(req._data.containsKey(ShareMacros.TYPE))
            typeJet = req._data.get(ShareMacros.TYPE);
        else
        {  
            outFalse(resp);
            return;
        }
        
        Map<String,String> kq = modJet.buyLevel(typeJet);
         
        JSONObject data = new JSONObject();
        data.putAll(kq);
        out(data.toJSONString(), resp);
    }
    
     public void get(ClientRequest req, HttpServletResponse resp)
    {
        String uid = req._uid;
        String key = KeysDefinition.getKeyJets(uid);
       
        ModelJet modJet = new ModelJet(uid);
        Map<String,String> jet = new HashMap<String,String>();
         jet = modJet.getJets();
         
         JSONObject data = new JSONObject();
         data.putAll(jet);
        
        out(data.toJSONString(), resp);
            return;
    }
     
     
}
